import React from 'react'
import { Form, Input, Button,message } from 'antd';
import {
    debounce
} from "lodash"
import {
    useNavigate
} from "react-router-dom"

function Login() {
    const navigate = useNavigate()
    const onFinish = (values) => {
        window.localStorage.setItem("token",JSON.stringify(values))
        success("登录成功",values)
        //重定向到 /
        navigate("/")
    };
    const success = (str) => {
        message.success(str);
    };
    const error = (str) => {
        message.error(str);
    };
    
    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
        error("失败了，不要再登录了")
    };
    return (
        <div>
            <Form
                name="basic"
                labelCol={{
                    span: 8,
                }}
                wrapperCol={{
                    span: 16,
                }}
                initialValues={{
                    remember: true,
                }}
                onFinish={debounce(onFinish,3000)}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
                >
                <Form.Item
                    label="Username"
                    name="username"
                    rules={[
                    {
                        required: true,
                        message: 'Please input your username!',
                    },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="Password"
                    name="password"
                    rules={[
                    {
                        required: true,
                        message: 'Please input your password!',
                    },
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item
                    wrapperCol={{
                    offset: 8,
                    span: 16,
                    }}
                >
                    <Button type="primary" htmlType="submit">
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default Login
